import Dialog from "../../../../component/vant/dialog/dialog";

let app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        spinShow: false,
        stockTakingId: null,
        stockTaking: {},
        tableData: [],
        //action-sheet
        actions: [
            {name: '新增', type: "add"},
            {name: '工作台', type: "homepage"},
        ],
        actionSheetShow: false,
    },
    queryData: function () {
        let _this = this
        app.formGet('/app/stock/taking/detail?id=' + this.data.stockTakingId, null).then(res => {
            _this.setData({
                spinShow: false
            });
            if (res.code === 0) {
                _this.setData({
                    stockTaking: res.data.stockTaking,
                    tableData: res.data.stockTakingDetailList
                });
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let stockTakingId = options.stockTakingId
        this.setData({
            stockTakingId: stockTakingId
        });
    },
    onShow: function () {
        this.queryData()
    },
    actionSelect: function (e) {
        let type = e.detail.type
        if (type == 'add') {
            wx.redirectTo({
                url: "/pages/stock/check/stockCheck"
            })
        } else if (type == 'copy') {
            wx.redirectTo({
                url: "/pages/instore/add/index?copyInstoreId=" + this.data.instoreId
            })
        } else if (type == 'cancel') {
            Dialog.confirm({
                title: '是否确认撤销该单据？',
                message: '撤销后将撤销相应的库存、账目，且撤销后不可恢复',
            })
                .then(() => {
                    this.handleCancel()
                })
                .catch(() => {
                    console.log("concel")
                });
        } else if (type == 'homepage') {
            wx.switchTab({
                url: "/pages/index/index"
            })
        }
    },
    moreFun: function () {
        this.setData({
            actionSheetShow: true
        })
    },
    actionCancel() {
        this.setData({actionSheetShow: false});
    },
    actionClose: function () {
        this.setData({actionSheetShow: false});
    },
    handlePrint: function () {

    },
    handleCancel: function () {
        let _this = this
        Dialog.confirm({
            title: '是否确认撤销该单据？',
            message: '盘点单撤销后，将会恢复库存'
        })
            .then(() => {
                let data = {}
                data.stockTakingId = this.data.stockTakingId
                app.formPost('/app/stock/taking/cancel', data).then(res => {
                    _this.setData({
                        spinShow: false
                    });
                    wx.stopPullDownRefresh()
                    if (res.code === 0) {
                        app.message(res.message, 'success')
                        setTimeout(function () {
                            wx.navigateTo({
                                url: '/pages/stock/taking/index'
                            })
                        }, 2000)
                    } else {
                        app.message(res.message, 'error')
                    }
                }).catch(e => {
                    _this.setData({
                        spinShow: false
                    });
                    app.message(e, 'error')
                })
            })
            .catch(() => {
                console.log("concel")
            });
    }
})
